package com.tengju.bff.interfaces.covercharge.service.bestsign.impl.contract;

import com.alibaba.fastjson.JSONObject;
import com.tengju.bff.interfaces.covercharge.constant.CoverChargeErrorCode;
import com.tengju.bff.interfaces.covercharge.entity.vo.contract.*;
import com.tengju.bff.interfaces.covercharge.handler.CoverChargeException;
import com.tengju.bff.interfaces.covercharge.service.bestsign.BestSignClient;
import com.tengju.bff.interfaces.covercharge.service.bestsign.constant.BestSignBizReqUrlEnum;
import com.tengju.bff.interfaces.covercharge.service.bestsign.convert.BestSignContractConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author liang_shi
 * @date 2020/7/13 16:52
 * @description 上上签合同服务实现
 */
@Slf4j
@Service
public class BestSignContractService {


    @Resource
    private BestSignClient bestSignClient;

    /**
     * 获取模板变量
     *
     * @param templateNo 模板编号
     * @return 结果
     */
    public List<TemplateVarsVo> getContractTemplateVars(String transId, String templateNo) {
        Map result = null;
        try {
            Map<String, Object> bizMap = BestSignContractConverter.packageGetTemplateVarsReq(templateNo);
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_2, bizMap, transId);
        } catch (CoverChargeException se) {
            throw se;
        } catch (Exception e) {
            log.error("模板变量获取异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "模板变量获取异常");
        }
        //结果检查
        String data = bestSignClient.checkResponse(result);
        return BestSignContractConverter.packageGetTemplateVarsRes(data);
    }


    /**
     * 通过模版生成合同文件
     *
     * @param tid 模板Id
     * @return 结果
     */
    @Deprecated
    public Map getCreateContractToken(String transId, String tid, JSONObject templateValues, String groupValues) {
        Map result = null;
        try {
            Map<String, Object> bizMap = BestSignContractConverter.packageGetCreateContractTokenReq(tid, templateValues, groupValues,bestSignClient.getAccount());
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_3, bizMap, transId);
        } catch (CoverChargeException se) {
            throw se;
        } catch (Exception e) {
            log.error("通过模版生成合同文件异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "通过模版生成合同文件异常");
        }
        return result;
    }


    /**
     * 上传模板变量创建合同
     *
     * @param req 场景合同
     * @return 结果
     */
    public String createContract(String transId, ContractCreateReq req) {
        Map result = null;
        try {
            Map<String, Object> bizMap = BestSignContractConverter.packageCreateContractReq(req,bestSignClient.getAccount());
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_4_1, bizMap, transId);
        } catch (CoverChargeException se) {
            throw se;
        } catch (Exception e) {
            log.error("通过模版创建合同异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "通过模版创建合同异常");
        }
        //结果检查
        String data = bestSignClient.checkResponse(result);
        return BestSignContractConverter.packageCreateContractRes(data);
    }


    /**
     * 生成用户签名/印章图片
     *
     * @param req 自动签请求
     */
    public void createUserSignatureImage(String transId, CreateUserSignatureReq req) {
        Map result = null;
        try {
            Map<String, Object> bizMap = BestSignContractConverter.packageCreateUserSignatureImageReq(req);
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_16, bizMap, transId);
        } catch (CoverChargeException se) {
            throw se;
        } catch (Exception e) {
            log.error("生成用户签名/印章图片异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "生成用户签名/印章图片异常");
        }
        //结果检查
        bestSignClient.checkBooleanResponse(result);
    }

    /**
     * 生成企业印章图片
     *
     * @param req 自动签请求
     */
    public void createCompanySignatureImage(String transId, CreateCompanySignatureReq req) {
        Map result = null;
        try {
            Map<String, Object> bizMap = BestSignContractConverter.packageCreateCompanySignatureImageReq(req);
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_17, bizMap, transId);
        } catch (CoverChargeException se) {
            throw se;
        } catch (Exception e) {
            log.error("生成用户签名/印章图片异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "生成用户签名/印章图片异常");
        }
        //结果检查
        bestSignClient.checkBooleanResponse(result);
    }

    /**
     * 用模版变量签署合同
     *
     * @param req 自动签请求
     */
    public void signContractByAuto(String transId, AutoSignContractReq req) {
        Map result = null;
        try {
            Map<String, Object> bizMap = BestSignContractConverter.packageAutoSignContractReq(req);
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_6, bizMap, transId);
        } catch (CoverChargeException se) {
            throw se;
        } catch (Exception e) {
            log.error("用模版变量签署合同异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "用模版变量签署合同异常");
        }
        //结果检查
        bestSignClient.checkBooleanResponse(result);
    }


    /**
     * 用模版变量的手动签
     *
     * @param req 手动签请求
     */
    public HandSignContractRes signContractByHand(String transId, HandSignContractReq req) {
        Map result = null;
        try {
            Map<String, Object> bizMap = BestSignContractConverter.packageHandSignContractReq(req);
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_7, bizMap, transId);
        } catch (CoverChargeException se) {
            throw se;
        } catch (Exception e) {
            log.error("用模版变量的手动签合同异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "用模版变量的手动签合同异常");
        }
        //结果检查
        String data = bestSignClient.checkResponse(result);
        return BestSignContractConverter.packageHandSignContractRes(data);
    }


    /**
     * 锁定并结束合同
     *
     * @param contractId 合同Id
     */
    public void lockContract(String transId, String contractId) {
        Map result = null;
        try {
            Map<String, Object> bizMap = BestSignContractConverter.packageLockContractReq(contractId);
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_14, bizMap, transId);
        } catch (CoverChargeException se) {
            throw se;
        } catch (Exception e) {
            log.error("锁定并结束合同异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "锁定并结束合同异常");
        }
        //结果检查
        bestSignClient.checkBooleanResponse(result);
    }


    /**
     * 查询合同信息
     *
     * @param contractId 合同Id
     */
    public ContractInfoRes queryContractInfo(String transId, String contractId) {
        Map result = null;
        try {
            Map<String, Object> bizMap = BestSignContractConverter.packageQueryContractInfoReq(contractId);
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_15, bizMap, transId);
        } catch (CoverChargeException se) {
            throw se;
        } catch (Exception e) {
            log.error("查询合同信息异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "查询合同信息异常");
        }
        //结果检查
        String data = bestSignClient.checkResponse(result);
        return BestSignContractConverter.packageContractInfoRes(data);
    }


    /**
     * 获取合同预览页URL
     *
     * @param contractId 合同Id
     * @return 预览URL
     */
    public String getContractPreviewUrl(String transId, String contractId, String account) {
        Map result = null;
        try {
            Map<String, Object> bizReq = BestSignContractConverter.packageGetContractPreviewUrlReq(contractId, account);
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_5, bizReq, transId);
        } catch (CoverChargeException se) {
            throw se;
        } catch (Exception e) {
            log.error("获取合同预览页URL异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "获取合同预览页URL异常");
        }
        //结果检查
        String data = bestSignClient.checkResponse(result);
        return null;
    }


    /**
     * 下载合同文件
     *
     * @param contractId 合同Id
     * @return 文件流
     */
    public byte[] contractDownload(String transId, String contractId) {
        byte[] result = null;
        try {
            String bizReq = BestSignContractConverter.packageContractDownloadReq(contractId);
            result = bestSignClient.sendGet(BestSignBizReqUrlEnum.BIZ_REQ_9, bizReq, transId);
        } catch (CoverChargeException se) {
            throw se;
        } catch (Exception e) {
            log.error("下载合同文件异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "下载合同文件异常");
        }
        return result;
    }


    /**
     * 上传用户签名/印章图片
     *
     * @param req 上传信息
     */
    public void contractUpdateImage(String transId, CreateCompanySignatureReq req) {
        Map result = null;
        try {
            Map<String, Object> bizMap = BestSignContractConverter.packageUpdateContractImageReq(req);
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_18, bizMap, transId);
        } catch (CoverChargeException se) {
            throw se;
        } catch (Exception e) {
            log.error("上传用户签名/印章图片异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "上传用户签名/印章图片异常");
        }
        //结果检查
        bestSignClient.checkBooleanResponse(result);
    }


}
